package com.sinosoft.hydra.um.services.component.dataCollect.readers;

import com.sinosoft.hydra.um.domain.model.LabDataParser;
import com.sinosoft.hydra.um.services.component.dataCollect.DataParser;
import com.sinosoft.hydra.um.services.component.dataCollect.DataResult;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/8/23.
 */
public class ExcelTwoReader extends ExcelReader {


    protected void readExcel(Workbook workbook, Date begin, Date end, DataParser parser, DataResult dataResult, LabDataParser labDataParser) {
        //循环每一页
        Sheet sheet = workbook.getSheetAt(1);
        List<String> titles;
        if (labDataParser.getTitleRowNum() == -1) {
            titles = parser.getTitles();
        } else {
            titles = readCell(sheet.getRow(labDataParser.getTitleRowNum()), labDataParser);
        }
        int last = labDataParser.getCellsNum() == -1 ? titles.size() : labDataParser.getCellsNum();
        dataResult.addTitles(titles, last, parser);
        //循环每一行
        for (Row row : sheet) {
            if (row.getRowNum() >= labDataParser.getStartRowNum()) {
                List<String> cells = readCell(row, labDataParser);
                if (!parser.valid(cells)) {
                    break;
                }
                dataResult.addData(parser.parse(cells, begin, end), last, parser);
            }
        }
    }
}
